package com.jianyun.jyzs.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.EngineeringResult;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/11/17.
 * 个人项目动态中的项目图片附件等
 */

public class JoinImageUtils {

    public static String erpRootUrl;

    public interface BitmapListListener {
        void bitListener(List<String> bitmaplist);
    }

    public static String bitmapPath = "";
    static int i=1;
    public static void getBitList(final Context context, List<EngineeringResult.EngineerBean.AttachmentBean> attachmentlist, final BitmapListListener listener) {

        //final String bcfHttpUrl = LoginCache.getInstance().getLoginCache().getBcfHttpUrl();
        erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl()+"DataService/browseImage.aspx?autoid=";
        final List<String> bitmaplist = new ArrayList<>();
        for (int i = 0; i <attachmentlist.size() ; i++) {
            EngineeringResult.EngineerBean.AttachmentBean attachmentBean=attachmentlist.get(i);
            String phonPath = attachmentBean.getPhonPath();
            String photoPath = attachmentBean.getPhotoPath();
            String videoPath = attachmentBean.getVideoPath();
            int type = attachType(phonPath, photoPath, videoPath);
            switch (type) {
                case 0://音频
                    bitmapPath = R.drawable.rce_ic_music + "";
                    break;
                case 1://图片
                    bitmapPath = erpRootUrl + attachmentBean.getId();
                    break;
                case 2://视频
                    bitmapPath = R.drawable.video_download_btn_nor + "";
                    break;
            }
            bitmaplist.add(bitmapPath);
        }
        Log.i("test","调用一次"+i);
        i++;
        listener.bitListener(bitmaplist);
        /*rx.Observable.from(attachmentlist).map(new Func1<EngineeringResult.EngineerBean.AttachmentBean, Bitmap>() {
            @Override
            public Bitmap call(EngineeringResult.EngineerBean.AttachmentBean attachmentBean) {
                String phonPath = attachmentBean.getPhonPath();
                String photoPath = attachmentBean.getPhotoPath();
                String videoPath = attachmentBean.getVideoPath();
                int type = attachType(phonPath, photoPath, videoPath);
                Log.i("test", "type:" + type);
                Bitmap bitmap = null;

                switch (type) {
                    case 0://音频
                        Drawable drawable = context.getResources().getDrawable(R.drawable.rce_ic_music);
                        BitmapDrawable bd = (BitmapDrawable) drawable;
                        bitmap = bd.getBitmap();
                        bitmapPath = R.drawable.rce_ic_music + "";
                        break;
                    case 1://图片
                        bitmap = getbitmap(ceshiUrl + attachmentBean.getId());
                        bitmapPath = ceshiUrl + attachmentBean.getId();
                        break;
                    case 2://视频
                        Drawable drawable2 = context.getResources().getDrawable(R.drawable.video_download_btn_nor);
                        BitmapDrawable bd2 = (BitmapDrawable) drawable2;
                        bitmap = bd2.getBitmap();
                        bitmapPath = R.drawable.video_download_btn_nor + "";
                        break;
                }
                return bitmap;
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Bitmap>() {
                    @Override
                    public void onCompleted() {
                        listener.bitListener(bitmaplist);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        bitmaplist.add(bitmapPath);
                    }
                });*/
    }

    private static int attachType(String phonPath, String photoPath, String videoPath) {
        int type = -1;
        if (!TextUtils.isEmpty(phonPath)) type = 0;
        if (!TextUtils.isEmpty(photoPath)) type = 1;
        if (!TextUtils.isEmpty(videoPath)) type = 2;
        return type;
    }

    public static Bitmap getbitmap(String imageUri) {
        // 显示网络上的图片
        Bitmap bitmap = null;
        try {
            URL myFileUrl = new URL(imageUri);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();

        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            bitmap = null;
        } catch (IOException e) {
            e.printStackTrace();
            bitmap = null;
        }
        return bitmap;
    }
}
